package org.example.service.domain.impl;

import org.example.service.domain.RedisServer;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.function.Supplier;

@SuppressWarnings("unchecked")
@Service
public class RedisServerImpl implements RedisServer {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    
    @Override
    public <T> T get(String key, Class<T> clazz) {
        return (T) redisTemplate.opsForValue().get(key);
    }

    @Override
    public <T> T get(String key, T defaultValue, Class<T> clazz) {
        T t = get(key, clazz);
        return null == t ? defaultValue : t;
    }

    @Override
    public <T> T get(String key, Supplier<T> supplier, Class<T> clazz) {
        T t = get(key, clazz);
        if (Objects.nonNull(t)) {
            return t;
        }
        return supplier.get();
    }

    @Override
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }
}
